//--- Projeto Domus
// interactionsource.com
// Koji Pereira, Andre Veloso e Marcos Paulo

/*
 Projeto Domus
 interactionsource.com
 Koji Pereira, Andre Veloso e Marcos Paulo

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/


package com.interactionsource.input.wiimoteosc;

import java.util.EventListener;
import java.util.Vector;

import org.wiigee.device.Wiimote;
import org.wiigee.event.AccelerationEvent;
import org.wiigee.event.AccelerationListener;
import org.wiigee.event.ButtonListener;
import org.wiigee.event.ButtonPressedEvent;
import org.wiigee.event.ButtonReleasedEvent;
import org.wiigee.event.GestureEvent;
import org.wiigee.event.GestureListener;
import org.wiigee.event.MotionStartEvent;
import org.wiigee.event.MotionStopEvent;
import org.wiigee.util.Log;

import com.interactionsource.event.ContinuousEvent;
import com.interactionsource.event.ContinuousEventListener;
import com.interactionsource.event.TextEvent;
import com.interactionsource.event.TextEventListener;
import com.interactionsource.event.TriggerEvent;
import com.interactionsource.event.TriggerEventListener;
import com.interactionsource.input.InputDevice;

public class WiimoteOSCDevice implements InputDevice, 
GestureListener, 
ButtonListener,
AccelerationListener {

	private WiimoteOSCWiigee wiigeeOSC;

	private int oscPort;

	public static final String GESTURE = "GESTURE";

	public static final String ACCELL_X = "ACCELL_X";
	public static final String ACCELL_Y = "ACCELL_Y";
	public static final String ACCELL_Z = "ACCELL_Z";

	public static final String BUTTON_A = "BUTTON_A";
	public static final String BUTTON_B = "BUTTON_B";
	public static final String BUTTON_1 = "BUTTON_1";
	public static final String BUTTON_2 = "BUTTON_2";
	public static final String BUTTON_HOME = "BUTTON_HOME";
	public static final String BUTTON_MINUS = "BUTTON_MINUS";
	public static final String BUTTON_PLUS = "BUTTON_PLUS";
	public static final String BUTTON_UP = "BUTTON_UP";
	public static final String BUTTON_DOWN = "BUTTON_DOWN";
	public static final String BUTTON_LEFT = "BUTTON_LEFT";
	public static final String BUTTON_RIGHT = "BUTTON_RIGHT";

	public static final String MOTION_START = "MOTION_START";
	public static final String MOTION_STOP = "MOTION_STOP";

	public static final double TRESHOLD_RECOGINITION =  0.9d;

	// Listeners, receive generated events
	protected Vector<TriggerEventListener> gestureTriggerEventListener = new Vector<TriggerEventListener>();

	protected ContinuousEventListener accellXContinuousEventListener;
	protected ContinuousEventListener accellYContinuousEventListener;
	protected ContinuousEventListener accellZContinuousEventListener;

	protected ContinuousEventListener accellXContinuousEventListener_2;
	protected ContinuousEventListener accellYContinuousEventListener_2;
	protected ContinuousEventListener accellZContinuousEventListener_2;

	protected TriggerEventListener buttonATriggerEventListener;
	protected TriggerEventListener buttonBTriggerEventListener;
	protected TriggerEventListener button1TriggerEventListener;
	protected TriggerEventListener button2TriggerEventListener;
	protected TriggerEventListener buttonHomeTriggerEventListener;
	protected TriggerEventListener buttonMinusTriggerEventListener;
	protected TriggerEventListener buttonPlusTriggerEventListener;
	protected TriggerEventListener buttonUpTriggerEventListener;
	protected TriggerEventListener buttonDownTriggerEventListener;
	protected TriggerEventListener buttonLeftTriggerEventListener;
	protected TriggerEventListener buttonRightTriggerEventListener;

	protected TriggerEventListener motionStartTriggerEventListener;
	protected TriggerEventListener motionStopTriggerEventListener;

	protected TriggerEventListener qtGesturesEventListener;
	
	protected TextEventListener textEventListener;

	protected int qtGestos = 0;

	private boolean isReconhecendoGesto = false;

	private boolean isReconhecendoGestoEnabled = false;

	private boolean isMoving = false;
	private boolean isIdle = false;
	private int idleCount = 0;

	private boolean isAccellXEnabled = false;
	private boolean isAccellYEnabled = false;
	private boolean isAccellZEnabled = false;

	public WiimoteOSCDevice (int OSCport) {

		this.oscPort = OSCport;

		this.wiigeeOSC = new WiimoteOSCWiigee(this.oscPort);

		this.wiigeeOSC.setTrainButton(Wiimote.BUTTON_A); 
		this.wiigeeOSC.setCloseGestureButton(Wiimote.BUTTON_HOME); 
		//this.wiigeeOSC.setRecognitionButton(Wiimote.BUTTON_B);
		this.wiigeeOSC.setRecognitionButton(Wiimote.MOTION);	


		this.wiigeeOSC.addGestureListener(this);
		this.wiigeeOSC.addButtonListener(this);
		this.wiigeeOSC.addAccelerationListener(this);


		this.wiigeeOSC.mostraSaida();


	}

	/**
	 * @return the oscPort
	 */
	public int getOscPort() {
		return oscPort;
	}

	/**
	 * @param oscPort the oscPort to set
	 */
	public void setOscPort(int oscPort) {
		this.oscPort = oscPort;
		this.wiigeeOSC.setOSCPort(this.oscPort);
	}

	public void addEventListenerByType(String eventName, String filePath, EventListener listener) {

		Log.write("WiimoteOSCDevice - Porta " + this.oscPort 
				+ " Adcionando Listener tipo:" + eventName);

		if (eventName.equals(ACCELL_X)) {
			addAccellXContinuousEventListener((ContinuousEventListener)listener);
			return;
		} 
		if (eventName.equals(ACCELL_Y)) {
			addAccellYContinuousEventListener((ContinuousEventListener)listener);
			return;
		}
		if (eventName.equals(ACCELL_Z)) {
			addAccellZContinuousEventListener((ContinuousEventListener)listener);
			return;
		}
		if (eventName.equals(BUTTON_A)) {
			addButtonATriggerEventListener((TriggerEventListener)listener);
			return;
		}
		if (eventName.equals(BUTTON_B)) {
			addButtonBTriggerEventListener((TriggerEventListener)listener);
			return;
		}
		if (eventName.equals(BUTTON_1)) {
			addButton1TriggerEventListener((TriggerEventListener)listener);
			return;
		}
		if (eventName.equals(BUTTON_2)) {
			addButton2TriggerEventListener((TriggerEventListener)listener);
			return;
		}
		if (eventName.equals(BUTTON_PLUS)) {
			addButtonPlusTriggerEventListener((TriggerEventListener)listener);
			return;
		}
		if (eventName.equals(BUTTON_HOME)) {
			addButtonHomeTriggerEventListener((TriggerEventListener)listener);
			return;
		}
		if (eventName.equals(BUTTON_MINUS)) {
			addButtonMinusTriggerEventListener((TriggerEventListener)listener);
			return;
		}
		if (eventName.equals(BUTTON_UP)) {
			addButtonUpTriggerEventListener((TriggerEventListener)listener);
			return;
		}
		if (eventName.equals(BUTTON_DOWN)) {
			addButtonDownTriggerEventListener((TriggerEventListener)listener);
			return;
		}		
		if (eventName.equals(BUTTON_LEFT)) {
			addButtonLeftTriggerEventListener((TriggerEventListener)listener);
			return;
		}
		if (eventName.equals(BUTTON_RIGHT)) {
			addButtonRightTriggerEventListener((TriggerEventListener)listener);
			return;
		}
		if (eventName.equals(MOTION_START)) {
			addMotionStartTriggerEventListener((TriggerEventListener)listener);
			return;
		}
		if (eventName.equals(MOTION_STOP)) {
			addMotionStopTriggerEventListener((TriggerEventListener)listener);
			return;
		}

		//caso nao seja nenhum dos eventos acima
		//Eh um evento de gesto
		if (filePath != null) {

			//Adciona o trigger
			addGestureTriggerEventListener((TriggerEventListener)listener);

			//Carrega o arquivo de gesto na memoria do Classificador
			String filename = filePath + eventName;
			this.loadConfiguration(filename);
		}




	}

	public void addQtGesturesEventListener( TriggerEventListener listener ) {
		this.qtGesturesEventListener = listener;

	}
	
	/**
	 * @param listener
	 */
	public void addTextEventListener( TextEventListener listener ) {
		this.textEventListener = listener;
	}

	public void addGestureTriggerEventListener( TriggerEventListener listener ) {
		this.gestureTriggerEventListener.add(listener);

	}

	public void addAccellXContinuousEventListener ( ContinuousEventListener listener ) {
		this.accellXContinuousEventListener = listener;

	}

	public void addAccellYContinuousEventListener ( ContinuousEventListener listener ) {
		this.accellYContinuousEventListener = listener;

	}

	public void addAccellZContinuousEventListener ( ContinuousEventListener listener ) {
		this.accellZContinuousEventListener = listener;

	}

	//----------------------

	public void addAccellXContinuousEventListener_2 ( ContinuousEventListener listener ) {
		this.accellXContinuousEventListener_2 = listener;
	}

	public void addAccellYContinuousEventListener_2 ( ContinuousEventListener listener ) {
		this.accellYContinuousEventListener_2 = listener;
	}

	public void addAccellZContinuousEventListener_2 ( ContinuousEventListener listener ) {
		this.accellZContinuousEventListener_2 = listener;
	}
	//-----------------------

	public void addButtonATriggerEventListener ( TriggerEventListener listener ) {
		this.buttonATriggerEventListener = listener;

	}

	public void addButtonBTriggerEventListener ( TriggerEventListener listener ) {
		this.buttonBTriggerEventListener = listener;

	}

	public void addButton1TriggerEventListener ( TriggerEventListener listener ) {
		this.button1TriggerEventListener = listener;

	}

	public void addButton2TriggerEventListener ( TriggerEventListener listener ) {
		this.button2TriggerEventListener = listener;

	}

	public void addButtonHomeTriggerEventListener ( TriggerEventListener listener ) {
		this.buttonHomeTriggerEventListener = listener;

	}

	public void addButtonMinusTriggerEventListener ( TriggerEventListener listener ) {
		this.buttonMinusTriggerEventListener = listener;

	}

	public void addButtonPlusTriggerEventListener ( TriggerEventListener listener ) {
		this.buttonPlusTriggerEventListener = listener;

	}

	public void addButtonUpTriggerEventListener ( TriggerEventListener listener ) {
		this.buttonUpTriggerEventListener = listener;

	}

	public void addButtonDownTriggerEventListener ( TriggerEventListener listener ) {
		this.buttonDownTriggerEventListener = listener;

	}

	public void addButtonLeftTriggerEventListener ( TriggerEventListener listener ) {
		this.buttonLeftTriggerEventListener = listener;

	}

	public void addButtonRightTriggerEventListener ( TriggerEventListener listener ) {
		this.buttonRightTriggerEventListener = listener;

	}

	public void addMotionStartTriggerEventListener ( TriggerEventListener listener ) {
		this.motionStartTriggerEventListener = listener;

	}

	public void addMotionStopTriggerEventListener ( TriggerEventListener listener ) {
		this.motionStopTriggerEventListener = listener;

	}

	/**
	 * @return the gestureTriggerEventListener
	 */
	public Vector<TriggerEventListener> getGestureTriggerEventListener() {
		return gestureTriggerEventListener;
	}

	/**
	 * @return the accellXContinuousEventListener
	 */
	public ContinuousEventListener getAccellXContinuousEventListener() {
		return accellXContinuousEventListener;
	}

	/**
	 * @return the accellYContinuousEventListener
	 */
	public ContinuousEventListener getAccellYContinuousEventListener() {
		return accellYContinuousEventListener;
	}

	/**
	 * @return the accellZContinuousEventListener
	 */
	public ContinuousEventListener getAccellZContinuousEventListener() {
		return accellZContinuousEventListener;
	}

	/**
	 * @return the buttonATriggerEventListener
	 */
	public TriggerEventListener getButtonATriggerEventListener() {
		return buttonATriggerEventListener;
	}

	/**
	 * @return the buttonBTriggerEventListener
	 */
	public TriggerEventListener getButtonBTriggerEventListener() {
		return buttonBTriggerEventListener;
	}

	/**
	 * @return the button1TriggerEventListener
	 */
	public TriggerEventListener getButton1TriggerEventListener() {
		return button1TriggerEventListener;
	}

	/**
	 * @return the button2TriggerEventListener
	 */
	public TriggerEventListener getButton2TriggerEventListener() {
		return button2TriggerEventListener;
	}

	/**
	 * @return the buttonHomeTriggerEventListener
	 */
	public TriggerEventListener getButtonHomeTriggerEventListener() {
		return buttonHomeTriggerEventListener;
	}

	/**
	 * @return the buttonMinusTriggerEventListener
	 */
	public TriggerEventListener getButtonMinusTriggerEventListener() {
		return buttonMinusTriggerEventListener;
	}

	/**
	 * @return the buttonPlusTriggerEventListener
	 */
	public TriggerEventListener getButtonPlusTriggerEventListener() {
		return buttonPlusTriggerEventListener;
	}

	/**
	 * @return the buttonUpTriggerEventListener
	 */
	public TriggerEventListener getButtonUpTriggerEventListener() {
		return buttonUpTriggerEventListener;
	}

	/**
	 * @return the buttonDownTriggerEventListener
	 */
	public TriggerEventListener getButtonDownTriggerEventListener() {
		return buttonDownTriggerEventListener;
	}

	/**
	 * @return the buttonLeftTriggerEventListener
	 */
	public TriggerEventListener getButtonLeftTriggerEventListener() {
		return buttonLeftTriggerEventListener;
	}

	/**
	 * @return the buttonRightTriggerEventListener
	 */
	public TriggerEventListener getButtonRightTriggerEventListener() {
		return buttonRightTriggerEventListener;
	}

	/**
	 * @return the motionStartTriggerEventListener
	 */
	public TriggerEventListener getMotionStartTriggerEventListener() {
		return motionStartTriggerEventListener;
	}

	/**
	 * @return the motionStopTriggerEventListener
	 */
	public TriggerEventListener getMotionStopTriggerEventListener() {
		return motionStopTriggerEventListener;
	}

	
	/**
	 * @param text
	 */
	public void fireTextEventListener( String text ) {
		if (this.textEventListener != null) {
			
			TextEvent te = new TextEvent(this);
			te.setText(text);
			
			this.textEventListener.textEventReceived(te);
			
		}
		
	}

	/* (non-Javadoc)
	 * @see org.wiigee.event.ButtonListener#buttonPressReceived(org.wiigee.event.ButtonPressedEvent)
	 * 	public static final int BUTTON_2 = 1;	
	 * 
	 * 
	 */
	public void buttonPressReceived(ButtonPressedEvent event) {
		//System.out.println("WiimoteOSCDevice - Recebeu Aperto Botao " + event.getButton() + 
		//		           " - portaOSC: " + this.oscPort);

		if (event.isCloseGestureInitEvent()) {
			//System.out.println("WiimoteOSCDevice - Recebeu Fechou Gesto");
		}

		if (event.isRecognitionInitEvent()) {
			//System.out.println("WiimoteOSCDevice - Recebeu Iniciou Reconhecimento de gestos");
		}

		if (event.isTrainInitEvent()) {
			//System.out.println("WiimoteOSCDevice - Recebeu Iniciou Treinamento de gestos");
		}		


		String nomearq;

		int button = event.getButton();

		TriggerEvent w = new TriggerEvent(event, 1);

		switch (button) {

		case Wiimote.BUTTON_1:
			if (button1TriggerEventListener != null) {
				this.button1TriggerEventListener.triggerEventReceived(w);
			}
			break;

		case Wiimote.BUTTON_2:
			if (button2TriggerEventListener != null) {
				this.button2TriggerEventListener.triggerEventReceived(w);
			}
			break;

		case Wiimote.BUTTON_A:
			if (buttonATriggerEventListener != null) {
				this.buttonATriggerEventListener.triggerEventReceived(w);
			}
			break;

		case Wiimote.BUTTON_B:
			if (buttonBTriggerEventListener != null) {
				this.buttonBTriggerEventListener.triggerEventReceived(w);
			}
			break;

		case Wiimote.BUTTON_DOWN:
			if (buttonDownTriggerEventListener != null) {
				this.buttonDownTriggerEventListener.triggerEventReceived(w);
			}
			break;

		case Wiimote.BUTTON_HOME:
			if (buttonHomeTriggerEventListener != null) {
				this.buttonHomeTriggerEventListener.triggerEventReceived(w);
			}

			this.qtGestos++;

			break;

		case Wiimote.BUTTON_LEFT:
			if (buttonLeftTriggerEventListener != null) {
				this.buttonLeftTriggerEventListener.triggerEventReceived(w);
			}
			break;

		case Wiimote.BUTTON_MINUS:
			if (buttonMinusTriggerEventListener != null) {
				this.buttonMinusTriggerEventListener.triggerEventReceived(w);
			}

			//DEBUG - salva arquivo config
			//nomearq = "/Users/andrevj/Dropbox/Projeto Circo/Java/Teste_wiigee/config/GESTURE_" + (this.qtGestos-1);
			//this.saveConfiguration(nomearq); 
			//DEBUG

			break;

		case Wiimote.BUTTON_PLUS:
			if (buttonPlusTriggerEventListener != null) {
				this.buttonPlusTriggerEventListener.triggerEventReceived(w);
			}

			//DEBUG - carrega arquivo config
			//nomearq = "/Users/andrevj/Dropbox/Projeto Circo/Java/Teste_wiigee/config/GESTURE_" + (this.qtGestos);
			//this.loadConfiguration(nomearq); 
			//DEBUG

			break;

		case Wiimote.BUTTON_RIGHT:
			if (buttonRightTriggerEventListener != null) {
				this.buttonRightTriggerEventListener.triggerEventReceived(w);
			}
			break;

		case Wiimote.BUTTON_UP:
			if (buttonUpTriggerEventListener != null) {
				this.buttonUpTriggerEventListener.triggerEventReceived(w);
			}
			break;

		}


	}

	/* (non-Javadoc)
	 * @see org.wiigee.event.ButtonListener#buttonReleaseReceived(org.wiigee.event.ButtonReleasedEvent)
	 */
	public void buttonReleaseReceived(ButtonReleasedEvent event) {
		int button = event.getButton();

		TriggerEvent w = new TriggerEvent(event, 0);

		switch (button) {

		case Wiimote.BUTTON_1:
			if (button1TriggerEventListener != null) {
				this.button1TriggerEventListener.triggerEventReceived(w);
			}
			break;

		case Wiimote.BUTTON_2:
			if (button2TriggerEventListener != null) {
				this.button2TriggerEventListener.triggerEventReceived(w);
			}
			break;

		case Wiimote.BUTTON_A:
			if (buttonATriggerEventListener != null) {
				this.buttonATriggerEventListener.triggerEventReceived(w);
			}
			break;

		case Wiimote.BUTTON_B:
			if (buttonBTriggerEventListener != null) {
				this.buttonBTriggerEventListener.triggerEventReceived(w);
			}
			break;

		case Wiimote.BUTTON_DOWN:
			if (buttonDownTriggerEventListener != null) {
				this.buttonDownTriggerEventListener.triggerEventReceived(w);
			}
			break;

		case Wiimote.BUTTON_HOME:
			if (buttonHomeTriggerEventListener != null) {
				this.buttonHomeTriggerEventListener.triggerEventReceived(w);
			}
			break;

		case Wiimote.BUTTON_LEFT:
			if (buttonLeftTriggerEventListener != null) {
				this.buttonLeftTriggerEventListener.triggerEventReceived(w);
			}
			break;

		case Wiimote.BUTTON_MINUS:
			if (buttonMinusTriggerEventListener != null) {
				this.buttonMinusTriggerEventListener.triggerEventReceived(w);
			}
			break;

		case Wiimote.BUTTON_PLUS:
			if (buttonPlusTriggerEventListener != null) {
				this.buttonPlusTriggerEventListener.triggerEventReceived(w);
			}
			break;

		case Wiimote.BUTTON_RIGHT:
			if (buttonRightTriggerEventListener != null) {
				this.buttonRightTriggerEventListener.triggerEventReceived(w);
			}
			break;

		case Wiimote.BUTTON_UP:
			if (buttonUpTriggerEventListener != null) {
				this.buttonUpTriggerEventListener.triggerEventReceived(w);
			}
			break;

		}


	}

	@Override
	public void gestureReceived(GestureEvent event) {

		//System.out.println("WiimoteOSCDevice - Recebeu Gesto ");

		if ( (event.getProbability() > TRESHOLD_RECOGINITION) && (event.isValid()) ) {

			//System.out.println("WiimoteOSCDevice - Evento de gesto disparado");


			
			this.fireTextEventListener("WiimoteOSCDevice - Gesture " + event.getId() + "" +
					" recognized at " + event.getProbability()*100 + "% probability");
			
			
			TriggerEvent w = new TriggerEvent(event, 1);

			if (this.gestureTriggerEventListener.size() > 0) {
				if (this.gestureTriggerEventListener.get(event.getId()) != null) {
					this.gestureTriggerEventListener.get(event.getId()).triggerEventReceived(w);
				}
			}

		} else { 
			
			this.fireTextEventListener("Gesture not recognized");
		}
	}

	@Override
	public void accelerationReceived(AccelerationEvent event) {

		//System.out.println("WiimoteOSCDevice - Recebeu aceleracao ");
		
		//this.fireTextEventListener("WiimoteOSCDevice - Recebeu aceleracao ");

		this.isMoving = true;

		ContinuousEvent x = new ContinuousEvent(event, (float)event.getX());
		if (this.accellXContinuousEventListener != null) {

			if (isAccellXEnabled) {

				this.accellXContinuousEventListener.continuousEventReceived(x);		
			}
		}
		if (this.accellXContinuousEventListener_2 != null) {
			this.accellXContinuousEventListener_2.continuousEventReceived(x);
		}

		ContinuousEvent y = new ContinuousEvent(event, (float)event.getY());
		if (this.accellYContinuousEventListener != null) {
			if (isAccellYEnabled) {
				this.accellYContinuousEventListener.continuousEventReceived(y);
			}
		}
		if (this.accellYContinuousEventListener_2 != null) {
			this.accellYContinuousEventListener_2.continuousEventReceived(y);
		}

		ContinuousEvent z = new ContinuousEvent(event, (float)event.getZ());
		if (this.accellZContinuousEventListener != null) {
			if (isAccellZEnabled) {
				this.accellZContinuousEventListener.continuousEventReceived(z);
			}
		}
		if (this.accellZContinuousEventListener_2 != null) {
			this.accellZContinuousEventListener_2.continuousEventReceived(z);
		}

	}

	@Override
	public void motionStartReceived(MotionStartEvent event) {

		if ( (isMoving) ){

			TriggerEvent w = new TriggerEvent(event, 1);
			//TODO removido por enquanto, para nao gerar eventos de saida
			if (this.motionStartTriggerEventListener != null) {
				//this.motionStartTriggerEventListener.triggerEventReceived(w);	
			}


			// TODO - Teste de trigger do inicio do processo de reconhecimento de gesto
			//por inicio de movimento
			//System.out.println("WiimoteOSCDevice - @@@@@@@ motionStartReceived FORCADO");

			if (this.isMoving) {

				if (event.isRecognitionInitEvent()) {

					//System.out.println("WiimoteOSCDevice - isRecognitionInitEvent TRUE");

					if (isReconhecendoGestoEnabled) {

						if (!isReconhecendoGesto) {
							System.out.println("_______________________________________");
							System.out.println("WiimoteOSCDevice - motionStartReceived ");
							
							
							this.fireTextEventListener("Gesture Recognition started");
							
							iniciaReconhecimentoGesto();
							isReconhecendoGesto = true;
						}
					}

				}
			}
		}

	}

	@Override
	public void motionStopReceived(MotionStopEvent event) {


		TriggerEvent w = new TriggerEvent(event, 1);
		//TODO removido por enquanto, para nao gerar eventos de saida
		if (this.motionStopTriggerEventListener != null) {
			//this.motionStopTriggerEventListener.triggerEventReceived(w);	
		}

		// TODO - Teste de trigger do termino do processo de reconhecimento de gesto
		//por fim de movimento
		//System.out.println("WiimoteOSCDevice - ### motionStopReceived FORCADO");


		if (isReconhecendoGestoEnabled) {
			if (isReconhecendoGesto) {
				terminaReconhecimentoGesto();
				System.out.println("WiimoteOSCDevice - terminaReconhecimentoGesto");
				System.out.println("_______________________________________");
				System.out.println("                                       ");
				
				this.fireTextEventListener("Gesture Recognition finished");
				
				isReconhecendoGesto = false;
				this.isMoving = false;
			}
		}




	}

	/**
	 * @return the wiigeeOSC
	 */
	public WiimoteOSCWiigee getWiigeeOSC() {
		return wiigeeOSC;
	}

	/**
	 * @param wiigeeOSC the wiigeeOSC to set
	 */
	public void setWiigeeOSC(WiimoteOSCWiigee wiigeeOSC) {
		this.wiigeeOSC = wiigeeOSC;
	}

	@Override
	public int loadConfiguration(String filename) {
		this.wiigeeOSC.carregaGesto(filename);
		qtGestos++;

		if (this.qtGesturesEventListener != null)
			this.qtGesturesEventListener.triggerEventReceived(new TriggerEvent(this, qtGestos));

		return 0;

	}

	@Override
	public int saveConfiguration(String filename) {
		int id = (this.qtGestos-1);
		this.wiigeeOSC.salvaGesto(id, filename);

		if (this.qtGesturesEventListener != null)
			this.qtGesturesEventListener.triggerEventReceived(new TriggerEvent(this, qtGestos));

		return 0;

	}

	public void iniciaTreinoGesto( ) {
		this.wiigeeOSC.iniciaTreinoGesto(this.wiigeeOSC.getTrainButton());	
		this.fireTextEventListener("Gesture capture started - Move Wiimote...");
	}

	public void terminaTreinoGesto ( ) {
		this.wiigeeOSC.terminaTreinoGesto(this.wiigeeOSC.getTrainButton());
		this.fireTextEventListener("Gesture capture finished.");
	}

	public void salvaGesto ( ) {
		this.wiigeeOSC.salvaTreinoGesto(this.wiigeeOSC.getCloseGestureButton());
		this.fireTextEventListener("# Gesture trained. Data Logged. Now save gesture");
		this.fireTextEventListener("or test recognition.");
	}

	public void iniciaReconhecimentoGesto ( ) {
		this.wiigeeOSC.iniciaReconhecimentoGesto(this.wiigeeOSC.getRecognitionButton());
	}

	public void terminaReconhecimentoGesto () {
		this.wiigeeOSC.terminaReconhecimentoGesto(this.wiigeeOSC.getRecognitionButton());
	}

	/**
	 * @return the isReconhecendoGestoEnabled
	 */
	public boolean isReconhecendoGestoEnabled() {
		return isReconhecendoGestoEnabled;
	}

	/**
	 * @param isReconhecendoGestoEnabled the isReconhecendoGestoEnabled to set
	 */
	public void setReconhecendoGestoEnabled(boolean isReconhecendoGestoEnabled) {
		this.isReconhecendoGestoEnabled = isReconhecendoGestoEnabled;
	}

	/**
	 * @return the isAccellXEnabled
	 */
	public boolean isAccellXEnabled() {
		return isAccellXEnabled;
	}

	/**
	 * @return the isAccellYEnabled
	 */
	public boolean isAccellYEnabled() {
		return isAccellYEnabled;
	}

	/**
	 * @return the isAccellZEnabled
	 */
	public boolean isAccellZEnabled() {
		return isAccellZEnabled;
	}

	/**
	 * @param isAccellXEnabled the isAccellXEnabled to set
	 */
	public void setAccellXEnabled(boolean isAccellXEnabled) {
		this.isAccellXEnabled = isAccellXEnabled;
	}

	/**
	 * @param isAccellYEnabled the isAccellYEnabled to set
	 */
	public void setAccellYEnabled(boolean isAccellYEnabled) {
		this.isAccellYEnabled = isAccellYEnabled;
	}

	/**
	 * @param isAccellZEnabled the isAccellZEnabled to set
	 */
	public void setAccellZEnabled(boolean isAccellZEnabled) {
		this.isAccellZEnabled = isAccellZEnabled;
	}

	/**
	 * @return the qtGestos
	 */
	public int getQtGestos() {
		return qtGestos;
	}

	/**
	 * @param qtGestos the qtGestos to set
	 */
	public void setQtGestos(int qtGestos) {
		this.qtGestos = qtGestos;
	}




}
